package com.ccit.order.service.impl;

import com.ccit.model.Reservation;
import com.ccit.model.vo.Reservation.ReservationVo;
import com.ccit.order.mapper.ReservationMapper;
import com.ccit.order.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

@Service
public class ReservationServiceImpl implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public List<Reservation> findAllReservation() {
        return reservationMapper.selectList(null);
    }

    @Override
    public void addReservation(ReservationVo param) {
        Reservation reservation = new Reservation();
        reservation.setUuid(UUID.randomUUID().toString().replace("-", ""));
        String time = new SimpleDateFormat("HH:mm:ss").format(param.getStartTime()) + " ~ " + new SimpleDateFormat("HH:mm:ss").format(param.getEndTime());
        reservation.setTime(time);
        reservationMapper.insert(reservation);
    }

    @Override
    public void deleteReservation(String uuid) {
        reservationMapper.deleteById(uuid);
    }

    @Override
    public void updateReservation(ReservationVo param) {
        String uuid = param.getUuid();
        String time = new SimpleDateFormat("HH:mm:ss").format(param.getStartTime()) + " ~ " + new SimpleDateFormat("HH:mm:ss").format(param.getEndTime());
        //mybatis-plus根据id更新
        Reservation reservation = new Reservation();
        reservation.setUuid(uuid);
        reservation.setTime(time);
        reservationMapper.updateById(reservation);
    }
}
